package cn.fay.chapter18;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class BufferToText {

	/**
	 * 要么输入时对其进行编码，要么输出时对其进行解码
	 */
	public static void main(String[] args) throws Exception {
		FileChannel fc=new FileOutputStream("data2.txt").getChannel();
		
		ByteBuffer encode = Charset.forName("UTF-8").encode("Some text");
		fc.write(encode);
		fc.close();
		
		fc=new FileInputStream(new File("data2.txt").getAbsoluteFile()).getChannel();
		ByteBuffer buffer=ByteBuffer.allocate((int) fc.size());
		fc.read(buffer);
		buffer.flip();
		System.out.println(Charset.forName("UTF-8").decode(buffer));
	}

}
